﻿using Kingdee.BOS;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Util;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WSDZ.KM.ZH.SalInvoiceDownloadFile6
{
    [Description("发票文件夹选择表单"), HotUpdate]
    public class ChoseInvoiceDirectoryDynamicForm:AbstractDynamicFormPlugIn
    {
        string txtname = string.Empty;
        public override void AfterBindData(EventArgs e)
        {
            base.AfterBindData(e);
            string sql = $@"/*dialect*/ select ExePath from ExeDefaultPath where UserName=@UserName";
            string path = DBUtils.ExecuteScalar<string>(Context,sql,"",new SqlParam("@UserName",KDDbType.String,Context.UserName));
            Model.SetValue("Fpath",path);
            View.UpdateView("Fpath");

            txtname = View.OpenParameter.GetCustomParameter("txtname").ToStringV1();
            Model.SetValue("FFPDD", txtname);
            View.UpdateView("FFPDD");
        }
        public override void ButtonClick(ButtonClickEventArgs e)
        {
            base.ButtonClick(e);
            if (e.Key.EqualsV1("FSubmit"))
            {
                string path = Model.GetValue("Fpath").ToStringV1();
                if (path.IsNullOrEmptyOrWhiteSpace())
                {
                    View.ShowErrMessage("运行路径不可为空!!!");
                    return;
                }
                else
                {
                    string sql = $@"/*dialect*/ select 1 from ExeDefaultPath where UserName=@UserName";
                    int count = DBUtils.ExecuteScalar<int>(Context, sql, 0, new SqlParam("@UserName", KDDbType.String, Context.UserName));
                    if (count>0)
                    {
                        sql = $@"/*dialect*/ update ExeDefaultPath set ExePath=@ExePath where UserName=@UserName";
                        DBUtils.Execute(Context, sql, new List<SqlParam>() {
                            new SqlParam("@ExePath", KDDbType.String, path),
                            new SqlParam("@UserName", KDDbType.String, Context.UserName)
                        });
                    }
                    else
                    {
                        sql = $@"/*dialect*/ insert into ExeDefaultPath(UserName,ExePath)values(@UserName,@ExePath)";
                        DBUtils.Execute(Context, sql, new List<SqlParam>() {
                            new SqlParam("@ExePath", KDDbType.String, path),
                            new SqlParam("@UserName", KDDbType.String, Context.UserName)
                        });
                    }
                    //string ExePath = @"C:\MyApps\DownloadInvoice\Sale.Client.exe";
                    string ExePath = Model.GetValue("Fpath").ToStringV1();
                    ExePath = $@"{ExePath}\Sale.Client.exe";
                    if (txtname.IsNullOrEmptyOrWhiteSpace())
                    {
                        View.ShowErrMessage("发票地址信息为空!!!");
                        return;
                    }
                    View.GetControl("F_Invoice_Link").InvokeControlMethod("SetClickFromServerOfParameter", ExePath, txtname);

                    //View.Close();
                }
            }
            else if (e.Key.EqualsV1("FClose"))
            {
                View.Close();
            }
        }
        //public override void BeforeClosed(BeforeClosedEventArgs e)
        //{
        //    base.BeforeClosed(e);
        //    string condition = objData.ToStringV1();
        //    this.View.ReturnToParentWindow(new FormResult(condition));
        //}
    }
}
